package com.mall.db.api;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeNewProductMapper;
import com.mall.pojo.SmsHomeBrand;
import com.mall.pojo.SmsHomeNewProduct;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

@DubboService
public class SmsHomeNewProductApiImpl implements SmsHomeNewProductApi{

    @Autowired
    private SmsHomeNewProductMapper homeNewProductMapper;

    /**
     * 查询推荐新品分页信息
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    @Override
    public Page<SmsHomeNewProduct> list(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeNewProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(productName), SmsHomeNewProduct::getProductName, productName)
                .eq(ObjectUtil.isNotEmpty(recommendStatus), SmsHomeNewProduct::getRecommendStatus, recommendStatus)
                .orderByDesc(SmsHomeNewProduct::getSort);
        return homeNewProductMapper.selectPage(page,wrapper);
    }

    /**
     * 根据一组id查询推荐新品信息
     * @param ids
     * @return
     */
    @Override
    public List<SmsHomeNewProduct> findProductByIds(Long[] ids) {
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeNewProduct::getId,ids);
        return homeNewProductMapper.selectList(wrapper);
    }

    /**
     * 修改推荐新品状态
     * @param product
     */
    @Override
    public Integer updateProduct(SmsHomeNewProduct product) {
        int count = homeNewProductMapper.updateById(product);
        return count;
    }

    /**
     * 根据商品id查询推荐新品信息
     * @param productId
     * @return
     */
    @Override
    public SmsHomeNewProduct findProductByProductId(Long productId) {
        LambdaQueryWrapper<SmsHomeNewProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SmsHomeNewProduct::getProductId,productId);
        return homeNewProductMapper.selectOne(wrapper);
    }

    /**
     * 添加推荐新品
     * @param product
     */
    @Override
    public void create(SmsHomeNewProduct product) {
        homeNewProductMapper.insert(product);
    }

    /**
     * 根据id查询推荐新品信息
     * @param id
     * @return
     */
    @Override
    public SmsHomeNewProduct findProductById(Integer id) {
        return homeNewProductMapper.selectById(id);
    }

    /**
     * 批量删除首页新品
     * @param ids
     * @return
     */
    @Override
    public Integer deleteByIds(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        return homeNewProductMapper.deleteBatchIds(idList);
    }
}
